package com.stedu.mall.orderservice.controller;

import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Order;
import com.stedu.mall.common.bean.OrderVo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.OrderService;
import com.stedu.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping
    public RespBean insert(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        orderVo.setUserId(userId);

        //添加订单
        orderService.insert(orderVo);

        return RespBean.ok("添加订单成功", orderVo);
    }

    @GetMapping
    public RespBean selectByPage(Order order, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        PageInfo<Order> pageInfo = orderService.selectByOrder(order, pageNum, pageSize);

        return RespBean.ok("查询成功", pageInfo);
    }

    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Order order = orderService.selectId(id, userId);
        return RespBean.ok("查询成功", order);
    }

    @GetMapping("/id/{id}")
    public RespBean selectId(@PathVariable("id") String id, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Order order = orderService.selectById(id, userId);
        return RespBean.ok("查询成功", order);
    }

    @PutMapping
    public RespBean update(@RequestBody Order order) {
        orderService.updateOrder(order);
        return RespBean.ok("修改成功");
    }

    @PutMapping("/{id}")
    public RespBean received(@PathVariable("id") String id) throws SteduException {

        Order order = orderService.selectByDId(id);
        order.setStatus(3);
        if (orderService.updateOrder(order)) {
            return RespBean.ok("已收货");
        } else {
            return RespBean.error("请联系管理员");
        }
    }

    @GetMapping("/user")
    public RespBean selectByUser(Integer pageNum, Integer pageSize, @RequestHeader("token") String token){
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Order condition = new Order();
        condition.setUserId(userId);

        PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);

        return RespBean.ok("查询成功", pageInfo);
    }

    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户Id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        orderVo.setUserId(userId);

        orderService.pay(orderVo);

        return RespBean.ok("支付成功");
    }

}
